What is @opentelemetry/context-zone-peer-dep?
@opentelemetry/context-zone-peer-dep is a package that provides context management capabilities using Zone.js. It is part of the OpenTelemetry project, which is a set of APIs, libraries, agents, and instrumentation to provide observability for applications.
What are @opentelemetry/context-zone-peer-dep's main functionalities?
Context Management
This feature allows you to manage context using Zone.js. The code sample demonstrates how to set the global context manager to use ZoneContextManager, create a new context, and run a function within that context.
const { context, setGlobalContextManager } = require('@opentelemetry/api');
const { ZoneContextManager } = require('@opentelemetry/context-zone-peer-dep');
// Set the global context manager to use ZoneContextManager
setGlobalContextManager(new ZoneContextManager());
// Create a new context
const ctx = context.active().setValue('key', 'value');
// Run a function within the context
context.with(ctx, () => {
console.log(context.active().getValue('key')); // Outputs: 'value'
});
Other packages similar to @opentelemetry/context-zone-peer-dep
@opentelemetry/context-async-hooks
@opentelemetry/context-async-hooks provides context management using Node.js async_hooks. It is similar to @opentelemetry/context-zone-peer-dep but is designed for use in Node.js environments, leveraging the async_hooks API for context propagation.
zone.js
zone.js is a library that provides execution context tracking for JavaScript. It is the underlying library used by @opentelemetry/context-zone-peer-dep for context management. While zone.js is more general-purpose, @opentelemetry/context-zone-peer-dep integrates it specifically for OpenTelemetry context management.
OpenTelemetry Context Zone Peer Dependency
This module provides Zone Context Manager with a peer dependency for zone-js for Web applications.
If you use Angular you already have the zone-js and you should use this package.
If you don't have your own zone-js please use @opentelemetry/context-zone
Installation
Please note that due to an issue with zone.js
, the ZoneContextManager
does not work with JS code targeting ES2017+
.
In order to use the ZoneContextManager
, please transpile back to ES2015
.
npm install --save @opentelemetry/context-zone-peer-dep
Usage
import { context, getSpan, setSpan } from '@opentelemetry/api';
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing';
import { WebTracerProvider } from '@opentelemetry/web';
import { ZoneContextManager } from '@opentelemetry/context-zone-peer-dep';
const providerWithZone = new WebTracerProvider();
providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
providerWithZone.register({
contextManager: new ZoneContextManager()
});
const webTracerWithZone = providerWithZone.getTracer('default');
const span1 = webTracerWithZone.startSpan('foo1');
context.with(setSpan(context.active(), span1, () => {
console.log('Current span is span1', getSpan(context.active()) === span1);
setTimeout(() => {
const span2 = webTracerWithZone.startSpan('foo2');
console.log('Current span is span1', getSpan(context.active()) === span1);
context.with(setSpan(context.active(), span2, () => {
console.log('Current span is span2', getSpan(context.active()) === span2);
setTimeout(() => {
console.log('Current span is span2', getSpan(context.active()) === span2);
}, 500);
});
console.log('Current span is span2', getSpan(context.active()) === span2);
}, 500);
console.log('Current span is span1', getSpan(context.active()) === span1);
});
Useful links
License
Apache 2.0 - See LICENSE for more information.